import { Config } from "../utils/Config";
import { JoyStickManager } from "../utils/JoyStickManager";
import { Vec3,Node } from "cc";


export class CamreaCtrl {
    static instance : CamreaCtrl = null;
    public static GetInstance()
    {
        if(!CamreaCtrl.instance)
        {
            CamreaCtrl.instance = new CamreaCtrl();
        }
        return CamreaCtrl.instance;
    }
    private mapCamera : Node = null;
    private player : Node = null;
    // 摄像机位置
    public lastMovePos : Vec3 = null;
    // ============================窗口大小========================================
    public winX : number = Config.GetInstance().GameWinX;
    public winY : number = Config.GetInstance().GameWinY;
    // ============================摄像机边界=======================================
    public maxX : number = Config.GetInstance().MapMaxXBlockCnt * Config.GetInstance().MapBlockFixSize;
    public maxY : number = Config.GetInstance().MapMaxYBlockCnt * Config.GetInstance().MapBlockFixSize;
    public minX : number = 0;
    public minY : number = 0;
    // ============================人物移动边界区域，摄像机固定========================
    public pLeftFromX : number = 0;
    public pLeftToX : number = -this.winX/2;
    public pRightFromX : number = this.maxX - this.winX;
    public pRightToX : number = this.maxX;
    public pBottomFromY : number = 0;
    public pBottomToY : number = -this.winY/2;
    public pUpFromY : number = this.maxY - this.winY;
    public pUpToY : number = this.maxY;
    // ==============================================================================
    initCamera(camera : Node,player : Node,offset : number = 0)
    {
        this.mapCamera = camera;
        this.player = player;
        this.mapCamera.setPosition(1280,1280,1000);
        this.player.setPosition(1280,1280,0);
        this.lastMovePos = this.mapCamera.getPosition().clone();
        //this.offset = offset;
        //console.log("pUpFromY: ",this.pUpFromY);
    }

    checkPosXIsMovedOfPlayer()
    {
        if(this.player)
        {
            let pos = this.player.getPosition();
            if(pos.x <= this.pLeftFromX && pos.x >= this.pLeftToX) return false;
            if(pos.x >= this.pRightFromX && pos.x <= this.pRightToX) return false;
            return true;
        }
        return false;
    }
    checkPosYIsMovedOfPlayer()
    {
        if(this.player)
        {
            let pos = this.player.getPosition();
            if(pos.y <= this.pBottomFromY && pos.y >= this.pBottomToY) return false;
            if(pos.y >= this.pUpFromY && pos.y <= this.pUpToY) return false;
            return true;
        }
        return false;
    }

    checkIsOutOfBoundaryX(obj : Node,_offsetX) 
    {
        let curPosX = obj.getPosition().x;
        if((curPosX + _offsetX) < (this.minX))
        {
            return 1;
        }
        if((curPosX + _offsetX) > (this.maxX - this.winX))
        {
            return 2;
        }
        return 0;
    }

    checkIsOutOfBoundaryY(obj : Node,_offsetY) 
    {
        let curPosY = obj.getPosition().y;
        if((curPosY + _offsetY) < (this.minY))
        {
            return 1;
        }
        if((curPosY + _offsetY) > (this.maxY - this.winY))
        {
            return 2;
        }
        return 0;
    }
    //更新mapCamera
    updateCamera () {
        // 
        this.lastMovePos = this.getCameraPos();

        let xIsMoving = this.checkPosXIsMovedOfPlayer();
        let yIsMoving = this.checkPosYIsMovedOfPlayer();
        let curPos = this.mapCamera.getPosition();
        let cx = Math.cos(JoyStickManager.GetInstance().joyStick.angle) * JoyStickManager.GetInstance().speed.x;
        let cy = Math.sin(JoyStickManager.GetInstance().joyStick.angle) * JoyStickManager.GetInstance().speed.y;
        //console.log("cx:",cx,"  cy:",cy)
        //检查范围
        let outX = this.checkIsOutOfBoundaryX(this.mapCamera,cx);
        let outY = this.checkIsOutOfBoundaryY(this.mapCamera,cy);

        if(outX == 0) curPos.x += cx;
        if(outX == 1) curPos.x = this.minX;
        if(outX == 2) curPos.x = this.maxX - this.winX;
        if(outY == 0) curPos.y += cy;
        if(outY == 1) curPos.y = this.minY;
        if(outY == 2) curPos.y = this.maxY - this.winY;

        if(!xIsMoving) curPos.x = this.mapCamera.getPosition().x;
        if(!yIsMoving) curPos.y = this.mapCamera.getPosition().y;
        
        this.mapCamera.setPosition(curPos);
    }

    getCX()
    {
        return this.mapCamera.getPosition().x - this.lastMovePos.x;
    }

    getCY()
    {
        return this.mapCamera.getPosition().y - this.lastMovePos.y;
    }

    getCameraPos()
    {
        return this.mapCamera.getPosition();
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
